package com.mdd.admin.esign;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mdd.admin.esign.comm.EsignHttpHelper;
import com.mdd.admin.esign.comm.EsignHttpResponse;
import com.mdd.admin.esign.enums.EsignRequestType;
import com.mdd.admin.esign.exception.EsignDemoException;
import com.mdd.common.entity.LegalCaseContract;
import com.mdd.common.entity.system.SystemAuthAdmin;
import com.mdd.common.entity.user.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Optional;

@Service
public class SignService {

    private static String eSignHost = EsignDemoConfig.EsignHost;
    private static String eSignAppId = EsignDemoConfig.EsignAppId;
    private static String eSignAppSecret = EsignDemoConfig.EsignAppSecret;
    public static String signFlowId = "";
    public static String fileId = "";
    @Value("${a.notifyUrl}")
    public String notifyUrl;
    /**
     * 获取合同解约链接
     *
     * @return
     */
    public static EsignHttpResponse signflowRescissionUrl(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/rescission-url";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{\n" +
                "    \"rescissionInitiator\": {\n" +
                "        \"orgInitiator\": {\n" +
                "            \"orgId\": \"0c5bd49248***5648bfbf\",\n" +
                "            \"transactor\": {\n" +
                "                \"psnId\": \"c7e002947***310541e7\"\n" +
                "            }\n" +
                "        }\n" +
                "    },\n" +
                "    \"signFlowConfig\": {\n" +
                "        \"chargeConfig\": {\n" +
                "            \"chargeMode\": 0\n" +
                "        },\n" +
                "        \"noticeConfig\": {\n" +
                "            \"noticeTypes\": \"1,2\"\n" +
                "        },\n" +
                "        \"notifyUrl\": \"https://xx.xx.xx/callback\"\n" +
                "    }\n" +
                "}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 核验区块链存证文件
     *
     * @return
     */
    public static EsignHttpResponse antchainFileInfoVerify() throws EsignDemoException {
        String apiaddr = "/v3/antchain-file-info/verify";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{\n" +
                "    \"fileHash\": \"bcdc3c***0477\",\n" +
                "    \"antTxHash\": \"11111111222222226c***fd1ed2f4\"\n" +
                "}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 获取区块链存证信息
     *
     * @return
     */
    public static EsignHttpResponse antchainFileInfo(String fileId) throws EsignDemoException {
        String apiaddr = "/v3/antchain-file-info";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{\n" +
                "    \"signFlowId\": \"\"\n" +
                "}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 核验合同文件签名有效性
     *
     * @return
     */
    public static EsignHttpResponse filesVerify(String fileId) throws EsignDemoException {
        String apiaddr = "/v3/files/" + fileId + "verify";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{\n" +
                "    \"signFlowId\": \"\"\n" +
                "}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 删除抄送方
     *
     * @return
     */
    public static EsignHttpResponse copiersDelete(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/copiers/delete";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{\n" +
                "    \"copiers\": [\n" +
                "        {\n" +
                "            \"copierPsnInfo\": {\n" +
                "                \"psnId\": \"\",\n" +
                "                \"psnAccount\": \"151****0101\"\n" +
                "            },\n" +
                "            \"copierOrgInfo\": {\n" +
                "                \"orgId\": \"\",\n" +
                "                \"orgName\": \"这是个抄送方的企业名称\"\n" +
                "            }\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 添加抄送方
     *
     * @return
     */
    public static EsignHttpResponse copiersAdd(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/copiers";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{\n" +
                "    \"copiers\": [\n" +
                "        {\n" +
                "            \"copierOrgInfo\": {\n" +
                "                \"orgName\": \"这是个抄送通知企业的名称\",\n" +
                "                \"orgId\": \"\"\n" +
                "            },\n" +
                "            \"copierPsnInfo\": {\n" +
                "                \"psnAccount\": \"153****7650\",\n" +
                "                \"psnId\": \"\"\n" +
                "            }\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 删除附属材料
     *
     * @return
     */
    public static EsignHttpResponse attachmentsDelete(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/attachments?fileIds=xxx1,xxx2";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = null;
        //请求方法
        EsignRequestType requestType = EsignRequestType.DELETE;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 追加附属材料
     *
     * @return
     */
    public static EsignHttpResponse attachmentsAdd(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/attachments";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{\n" +
                "    \"attachmentList\": [\n" +
                "        {\n" +
                "            \"fileId\": \"ea3151***a53d3f4c\",\n" +
                "            \"fileName\": \"入职手册.pdf\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 删除待签文件
     *
     * @return
     */
    public static EsignHttpResponse unsignedFilesDelete(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/unsigned-files?fileIds=xxx1,xxx2";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = null;
        //请求方法
        EsignRequestType requestType = EsignRequestType.DELETE;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 追加待签文件
     *
     * @return
     */
    public static EsignHttpResponse unsignedFilesAdd(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/unsigned-files";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{\n" +
                "    \"unsignedFiles\": [\n" +
                "        {\n" +
                "            \"fileId\": \"ea3151a8d***a53d3f4c\",\n" +
                "            \"fileName\": \"入职证明.pdf\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 删除签署区
     *
     * @return
     */
    public static EsignHttpResponse signFieldsDelete(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/signers/sign-fields?signFieldIds=xxx1,xxx2";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = null;
        //请求方法
        EsignRequestType requestType = EsignRequestType.DELETE;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 追加签署区
     *
     * @return
     */
    public static EsignHttpResponse signFieldsAdd(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/signers/sign-fields";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{\n" +
                "    \"signers\": [\n" +
                "        {\n" +
                "            \"noticeConfig\": {\n" +
                "                \"noticeTypes\": \"1\"\n" +
                "            },\n" +
                "            \"psnSignerInfo\": {\n" +
                "                \"psnAccount\": \"153****0000\"\n" +
                "            },\n" +
                "            \"signConfig\": {\n" +
                "                \"forcedReadingTime\": 10,\n" +
                "                \"signOrder\": 2\n" +
                "            },\n" +
                "            \"signFields\": [\n" +
                "                {\n" +
                "                    \"customBizNum\": \"202201010001\",\n" +
                "                    \"fileId\": \"0e99de7c***09db2cd69\",\n" +
                "                    \"remarkSignFieldConfig\": {\n" +
                "                        \"aiCheck\": 0,\n" +
                "                        \"freeMode\": true,\n" +
                "                        \"movableSignField\": true,\n" +
                "                        \"remarkContent\": \"我已阅读并知悉\",\n" +
                "                        \"remarkFontSize\": 20,\n" +
                "                        \"signFieldHeight\": 100,\n" +
                "                        \"signFieldPosition\": {\n" +
                "                            \"acrossPageMode\": \"\",\n" +
                "                            \"positionPage\": \"1\",\n" +
                "                            \"positionX\": 100,\n" +
                "                            \"positionY\": 100\n" +
                "                        },\n" +
                "                        \"signFieldWidth\": 100\n" +
                "                    },\n" +
                "                    \"signFieldType\": 1\n" +
                "                }\n" +
                "            ],\n" +
                "            \"signerType\": 0\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 查询签署流程列表
     *
     * @return
     */
    public static EsignHttpResponse signFlowList() throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/sign-flow-list";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{\n" +
                "    \"operator\": {\n" +
                "        \"psnAccount\": \"18******34\",\n" +
                "        \"psnId\": \"\"\n" +
                "    },\n" +
                "    \"pageNum\": 1,\n" +
                "    \"pageSize\": 20,\n" +
                "    \"signFlowStartTimeFrom\": 1648801671000,\n" +
                "    \"signFlowStartTimeTo\": 1651393671000,\n" +
                "    \"signFlowStatus\": [\n" +
                "        1,\n" +
                "        2\n" +
                "    ]\n" +
                "}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 查询签署流程详情
     *
     * @return
     */
    public static EsignHttpResponse signFlowDetail(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/detail";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = null;
        //请求方法
        EsignRequestType requestType = EsignRequestType.GET;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 催签流程中签署人
     *
     * @return
     */
    public static EsignHttpResponse signFlowUrge(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/urge";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{\n" +
                "    \"noticeTypes\": \"1\",\n" +
                "    \"urgedOperator\": {\n" +
                "        \"psnAccount\": \"183****0101\"\n" +
                "    }\n" +
                "}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 延期签署截止时间
     *
     * @return
     */
    public static EsignHttpResponse signFlowDelay(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/delay";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = null;
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 撤销签署流程
     *
     * @return
     */
    public static EsignHttpResponse signFlowRevoke(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/revoke";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 完结签署流程
     *
     * @param signFlowId
     * @return
     */
    public static EsignHttpResponse signFlowFinish(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + SignService.signFlowId + "/finish";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = "{}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 开启签署流程     *
     *
     * @param signFlowId
     * @return
     */
    public static EsignHttpResponse signFlowStart(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + SignService.signFlowId + "/start";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = null;
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * 下载已签署文件及附属材料     *
     *
     * @return
     * @throws EsignDemoException
     */
    public static EsignHttpResponse fileDownloadUrl(String signFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + signFlowId + "/file-download-url";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm = null;
        //请求方法
        EsignRequestType requestType = EsignRequestType.GET;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }


    /**
     * 获取批量签页面链接（多流程）
     *
     * @return
     */
    public static EsignHttpResponse batchSignUrl() throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/batch-sign-url";
        String jsonParm = "{\n" +
                "    \"operatorId\":\"0f375***********5738d976\",\n" +
                "    \"redirectUrl\":\"https://www.esign.cn\",\n" +
                "    \"signFlowIds\":[\"1fc79b***********1f2d5fd\"]\n" +
                "}";
        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成请求签名鉴权方式的Header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * pc调解员获取合同文件签署链接
     *
     * @return
     * @throws EsignDemoException
     */
    public static EsignHttpResponse Url(SystemAuthAdmin systemAuthAdmin,String SignFlowId) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + SignFlowId + "/sign-url";
        JsonObject jsonParmObj = new JsonObject();
        jsonParmObj.addProperty("clientType", "ALL");//签署客户端类型 all支持全部
        jsonParmObj.addProperty("needLogin", false); //不需登陆

        //签署操作人
        JsonObject operatorObj = new JsonObject();
        operatorObj.addProperty("psnAccount", systemAuthAdmin.getMobile());
        jsonParmObj.add("operator", operatorObj);

        jsonParmObj.addProperty("urlType", 2);//可通过链接签署

        String jsonParm = jsonParmObj.toString();

        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成请求签名鉴权方式的Header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }
    /**
     * app获取合同文件签署链接
     *
     * @return
     * @throws EsignDemoException
     */
    public static EsignHttpResponse signUrl(User user) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + user.getSignFlowId() + "/sign-url";
        JsonObject jsonParmObj = new JsonObject();
        jsonParmObj.addProperty("clientType", "ALL");//签署客户端类型 all支持全部
        jsonParmObj.addProperty("needLogin", false); //不需登陆

        //签署操作人
        JsonObject operatorObj = new JsonObject();
        operatorObj.addProperty("psnAccount", user.getMobile());
        jsonParmObj.add("operator", operatorObj);

        jsonParmObj.addProperty("urlType", 2);//可通过链接签署

        String jsonParm = jsonParmObj.toString();

        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成请求签名鉴权方式的Header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

    /**
     * app获取合同文件预览链接
     *
     * @return
     * @throws EsignDemoException
     */
    public static EsignHttpResponse signUrlOne(LegalCaseContract selectOne) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + selectOne.getSignFlowId() + "/sign-url";
        JsonObject jsonParmObj = new JsonObject();
        jsonParmObj.addProperty("clientType", "ALL");//签署客户端类型 all支持全部
        jsonParmObj.addProperty("needLogin", false); //不需登陆
//        jsonParmObj.addProperty("appScheme", "fawuapp://"); //唤起app

        //签署操作人
        JsonObject operatorObj = new JsonObject();
        String fileJson = selectOne.getFileJson();
        Gson gson = new Gson();
        Map<String, Object> fileMap = gson.fromJson(fileJson, new TypeToken<Map<String, Object>>() {
        }.getType());
        String aentrustingPhone = Optional.ofNullable((String) fileMap.get("entrusting_mobile")).orElse("");//委托方手机号
        if (!StringUtils.isEmpty(aentrustingPhone)) {
            operatorObj.addProperty("psnAccount", aentrustingPhone);
        } else {
            operatorObj.addProperty("psnAccount", selectOne.getUserMobile());
        }
        jsonParmObj.add("operator", operatorObj);

        jsonParmObj.addProperty("urlType", 1);//可通过链接签署

        String jsonParm = jsonParmObj.toString();

        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成请求签名鉴权方式的Header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }
    /**
     * app获取和解书文件预览链接
     *
     * @return
     * @throws EsignDemoException
     */
    public static EsignHttpResponse signUrlAppPreviewSettlement(User user) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + user.getSignFlowId() + "/sign-url";
        JsonObject jsonParmObj = new JsonObject();
        jsonParmObj.addProperty("clientType", "ALL");//签署客户端类型 all支持全部
        jsonParmObj.addProperty("needLogin", false); //不需登陆
//        jsonParmObj.addProperty("appScheme", "fawuapp://"); //唤起app

        //签署操作人
        JsonObject operatorObj = new JsonObject();
        operatorObj.addProperty("psnAccount", user.getMobile());
        jsonParmObj.add("operator", operatorObj);

        jsonParmObj.addProperty("urlType", 1);//可通过链接签署

        String jsonParm = jsonParmObj.toString();

        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成请求签名鉴权方式的Header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }
    /**
     * pc案件合同获取预览链接
     *
     * @return
     * @throws EsignDemoException
     */
    public static EsignHttpResponse signUrlTwo(User user, LegalCaseContract selectOne) throws EsignDemoException {
        String apiaddr = "/v3/sign-flow/" + user.getSignFlowId() + "/sign-url";
        JsonObject jsonParmObj = new JsonObject();
        jsonParmObj.addProperty("clientType", "ALL");//签署客户端类型 all支持全部
        jsonParmObj.addProperty("needLogin", false); //不需登陆

        //签署操作人
        JsonObject operatorObj = new JsonObject();
        String fileJson = selectOne.getFileJson();
        Gson gson = new Gson();
        Map<String, Object> fileMap = gson.fromJson(fileJson, new TypeToken<Map<String, Object>>() {
        }.getType());
        String aentrustingPhone = Optional.ofNullable((String) fileMap.get("entrusting_mobile")).orElse("");//委托方手机号
        if (!StringUtils.isEmpty(aentrustingPhone)) {
            operatorObj.addProperty("psnAccount", aentrustingPhone);
        } else {
            operatorObj.addProperty("psnAccount", user.getMobile());
        }
        jsonParmObj.add("operator", operatorObj);

        jsonParmObj.addProperty("urlType", 1);//可通过链接签署

        String jsonParm = jsonParmObj.toString();

        //请求方法
        EsignRequestType requestType = EsignRequestType.POST;
        //生成请求签名鉴权方式的Header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
    }

}
